package com.example.system.service;

import com.example.system.dao.SysBannerDao;
import com.example.system.entity.SysBanner;
import com.example.system.model.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

/**
 * author: adnaan
 * create: 2023-11-02 08:24
 * Description:
 */

@Service
public class SysBannerService {

    @Autowired
    private SysBannerDao sysBannerDao;

    public ApiResponse findByStatus() {
        Criteria criteriaType = Criteria.where("status").is("开启");
        Criteria orCriteria = new Criteria();
        orCriteria.orOperator(criteriaType);
        Query query = new Query(orCriteria);
        if (query != null) {
            return ApiResponse.success(sysBannerDao.get(query));
        } else {
            return ApiResponse.error("500", "获取失败");
        }
    }

    public ApiResponse addBanner(SysBanner banner) {
        return sysBannerDao.addBanner(banner);
    }

    public ApiResponse delBanner(String bannerId) {
        Query query = new Query(Criteria.where("id").is(bannerId));
        return sysBannerDao.delete(query);
    }

    public ApiResponse updateBanner(SysBanner banner) {
        Query query = new Query(Criteria.where("id").is(banner.getId()));
        Update update=new Update();
        update.set("image",banner.getImage());
        update.set("status",banner.getStatus());
        return sysBannerDao.update(query,update);
    }

    public ApiResponse findPaginated(Integer currentPage, Integer pageSize) {
        return sysBannerDao.findPaginated(currentPage, pageSize);
    }
}
